package com.matang;

import java.io.File;

import com.util.UserUtil;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.provider.Settings.Secure;
import android.util.Log;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MyWebViewClient extends WebViewClient {
	@Override
	public void onReceivedSslError(WebView view, SslErrorHandler handler,
			SslError error) {
		// TODO Auto-generated method stub
		handler.cancel();// Ĭ�ϵĴ��?ʽ��WebView��ɿհ�ҳ
		handler.proceed();// ����֤��
		// handleMessage(Message msg);// ������
		// super.onReceivedSslError(view, handler, error);
	}

	private Context context;

	public MyWebViewClient(Context context) {
		this.context = context;
	}

	@Override
	public void onPageStarted(WebView view, String url, Bitmap favicon) {

		super.onPageStarted(view, url, favicon);
	}

	@Override
	public boolean shouldOverrideUrlLoading(WebView view, String url) {
		String logout = "http://mm.tonggo.net/logout.aspx";
		Log.e("---shouldOverrideUrlLoading", url);
		if (logout.contains(url)) {//如果是退出 
			UserUtil.clearUser();
			String filepath =context.getFilesDir().getAbsolutePath() + "/data/"+ "msg.txt";
			File file = new File(filepath);
			if (file.exists()) {
				boolean isdelete = file.delete();
				if (isdelete) {
					Log.i("test", "删除文件成功");
				}
			}
		}
		view.loadUrl(url);
		return true;
	}

	@SuppressLint("DefaultLocale")
	@Override
	public void onPageFinished(WebView view, String url) {

		if (url.toLowerCase().startsWith("http://mm.tonggo.net/getticket.aspx")) {
			// ��java�е���js����
			view.loadUrl("javascript:window.local_obj.showSource(document.getElementsByTagName('title')[0].innerHTML);");
		}
		if (url.toLowerCase().startsWith(
				"http://mm.tonggo.net/checkticket.aspx")) {
			view.loadUrl("javascript:window.check_obj.showSource(document.getElementsByTagName('title')[0].innerHTML);");
		}
		Log.e("---onPageFinished", url);
		super.onPageFinished(view, url);
	}

	@Override
	public void onReceivedError(WebView view, int errorCode,
			String description, String failingUrl) {

		super.onReceivedError(view, errorCode, description, failingUrl);
	}

}
